package com.vmj.classfile;

import com.vmj.Command;

import java.io.IOException;

/**
 * Created by yangshoujun on 2017/8/5 下午8:13.
 */
public class Classpath {

    private Command command;

    private ClassLoader bootstrapClassloader;
    private ClassLoader extendsClassloader;
    private ClassLoader applicationClassloader;

    public Classpath(Command cmd) {
        this.command = cmd;
        bootstrapClassloader = new BootstrapClassLoader(cmd);
        applicationClassloader = new DirectoryClassLoader(cmd);
    }

    public byte[] loadClass(String className) throws IOException, ClassNotFoundException {
        byte[] bytes = bootstrapClassloader.loadClass(className);
        // TODO 从ext目录、classpath目录、文件中加载类
        if(bytes == null) {
            bytes = applicationClassloader.loadClass(className);
        }
        if(bytes == null) {
            throw new ClassNotFoundException("class " + className + " not found by vmj");
        }
        return bytes;
    }
}
